clear; sca;
[xax, yax] = meshgrid(1:224, 1:224);
rax = xax + 1i * yax;

radius = 6;  
number_sets = 1:15;
image_iter = 20;
color_num = 50;

colors = rand(color_num, 3) * 0.8 + 0.2;

main_output_dir = 'connected_control2_images';
if ~exist(main_output_dir, 'dir')
    mkdir(main_output_dir);
end

connected_image_control2 = zeros(224, 224, image_iter, length(number_sets), color_num);

for cc = 1:color_num
    stim_color = colors(cc, :);
    hsv_color = rgb2hsv(stim_color);
    hsv_color(2) = hsv_color(2) * 0.4;
    hsv_color(3) = min(1, hsv_color(3) * 1.8);
    bg_color = hsv2rgb(hsv_color);

    stim_color_255 = round(stim_color * 255);
    bg_color_255 = round(bg_color * 255);
    color_diff = sqrt(sum((stim_color_255 - bg_color_255).^2));

    if color_diff < 100
        luminance = 0.2126 * stim_color(1) + 0.7152 * stim_color(2) + 0.0722 * stim_color(3);
        if luminance > 0.5
            bg_color = [0.1, 0.1, 0.1];
        else
            bg_color = [0.9, 0.9, 0.9];
        end
        bg_color_255 = round(bg_color * 255);
    end

    folder_name = sprintf('color_%d_%.3f_%.3f_%.3f_diff%.0f', cc, stim_color(1), stim_color(2), stim_color(3), color_diff);
    folder_path = fullfile(main_output_dir, folder_name);
    if ~exist(folder_path, 'dir')
        mkdir(folder_path);
    end

    for tt = 1:length(number_sets)
        nn = number_sets(tt);  % 生成 nn 个连接刺激
        for ii = 1:image_iter

            % 设置凸包为五边形
            theta = randi(180)*pi/180;
            xtmpp = zeros(1,5); ytmpp = zeros(1,5);
            for thetaind = 1:5
                xtmpp(thetaind) = round(224/2)+110*cos(theta+(thetaind-1)*2*pi/5);
                ytmpp(thetaind) = round(224/2)+110*sin(theta+(thetaind-1)*2*pi/5);
            end

            % 初始化变量
            locmtmp = [];
            x1tmp = []; y1tmp = [];
            x2tmp = []; y2tmp = [];
            recttmp = []; circletmp = []; ellipsetmp = [];
            triangle1tmp = []; triangle2tmp = [];

            while length(locmtmp) < nn
                tmp = rand();
                loc1 = 5*ceil(radius)+randi(224-10*ceil(radius)) + ...
                    1i*(5*ceil(radius)+randi(224-10*ceil(radius)));
                x1 = real(loc1); y1 = imag(loc1);

                if tmp > 0.75
                    [x2, y2, locm, shape] = creatanconnectedRect(radius,x1,y1);
                    shape_ok = true;
                elseif tmp > 0.5
                    [x2, y2, locm, shape] = creatanconnectedCircle(radius,x1,y1);
                    shape_ok = true;
                elseif tmp > 0.25
                    [x2, y2, locm, shape] = creatanconnectedEllipse(radius,x1,y1);
                    shape_ok = true;
                else
                    [x2, y2, locm, shape1, shape2] = creatanconnectedTriangle(radius,x1,y1);
                    shape_ok = true;
                end

                distancestmp = abs(locm - locmtmp);
                radistmp = 6 * radius;
                okToAdd = isempty(distancestmp) || all(distancestmp > radistmp);
                IN = inpolygon(real(locm), imag(locm), xtmpp, ytmpp);

                if shape_ok && okToAdd && IN
                    locmtmp = [locmtmp, locm];
                    x1tmp = [x1tmp, x1]; y1tmp = [y1tmp, y1];
                    x2tmp = [x2tmp, x2]; y2tmp = [y2tmp, y2];

                    if tmp > 0.75
                        recttmp = [recttmp, shape];
                    elseif tmp > 0.5
                        circletmp = [circletmp, shape];
                    elseif tmp > 0.25
                        ellipsetmp = [ellipsetmp, shape];
                    else
                        triangle1tmp = [triangle1tmp, shape1];
                        triangle2tmp = [triangle2tmp, shape2];
                    end
                end
            end

            try
                [w, ~] = Screen('OpenWindow', max(Screen('Screens')), bg_color_255, [0 0 224 224]);

                if ~isempty(recttmp)
                    Screen('FillRect', w, stim_color_255', recttmp);
                end
                if ~isempty(circletmp)
                    Screen('DrawDots', w, circletmp, 2*radius, stim_color_255', [], 2);
                end
                if ~isempty(ellipsetmp)
                    Screen('FillOval', w, stim_color_255', ellipsetmp);
                end
                if size(triangle1tmp,2) > 0
                    for jj = 1:2:size(triangle1tmp,2)
                        Screen('FillPoly', w, stim_color_255', triangle1tmp(:,jj:jj+1));
                        Screen('FillPoly', w, stim_color_255', triangle2tmp(:,jj:jj+1));
                    end
                end
                for ll = 1:nn
                    Screen('DrawLine', w, stim_color_255', x1tmp(ll), y1tmp(ll), x2tmp(ll), y2tmp(ll));
                end

                Screen('Flip', w);
                imgtmp = Screen('GetImage', w, [], [], 1);
                connected_image_control2(:,:,ii,tt,cc) = rgb2gray(imgtmp);

                filename = sprintf('shape_nn%d_img%d.png', nn, ii);
                imwrite(imgtmp, fullfile(folder_path, filename));
                sca;
            catch
                sca
            end
        end
    end
end

connected_vgg_control2_color = connected_image_control2;
save connected_vgg_control2_color.mat connected_vgg_control2_color -v7.3
